// pages/teacher-detail/index.js
const handler = require("../../utils/function.js");
const { $Message } = require('../../dist/base/index');
// var dateTimePicker = require('../../dist/base/index');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    spinShow: true,
    showAppointment:true,
    visible5: false,
    actions5: [
      {
        name: '取消'
      },
      {
        name: '支付',
        color: '#19be6b',
        // loading: false
      }
    ],
    orderGoing: false,
    showTimeContainer: false,
    date: ["今天", "明天", "后台"],//时间选择器
    hours: [],
    minute: [],
    name:null,
    phone:null,
    time:null,
    address:null,
    appointment_money:200,
    give_money:10,
    tearcherData:[],
    wallet:[{title:"10元",val:"10"},{title:"20元",val:"20"},{title:"50元",val:"50"},{title:"100元",val:"100"},{title:"自定义",val:'0'}],
    wallet_id:0,
    is_show:false,
    dsprice:10,
    less_price:0,
    total_price:0,
 
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,//时间选择器
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: 2000,
    endYear: 2050,
    name: '',
    lesstime:'',
    less_address:'',
    phone: '',
    payPara :[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tid = options.tearchid;
    this.setData({
      tearch_id:tid
    })
    this.initData(tid);
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = handler.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = handler.dateTimePicker(this.data.startYear, this.data.endYear);
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = handler.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = handler.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();

    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime
    });

    var time = this.data.dateTimeArray1[0][this.data.dateTime1[0]] + "-" + this.data.dateTimeArray1[1][this.data.dateTime1[1]] + "-" + this.data.dateTimeArray1[2][this.data.dateTime1[2]] + " " + this.data.dateTimeArray1[3][this.data.dateTime1[3]] + ":" + this.data.dateTimeArray1[4][this.data.dateTime1[4]];
    
    var timestamp2 = Date.parse(new Date(time));
    timestamp2 = timestamp2 / 1000;

    this.setData({ lesstime: timestamp2 })
    console.log(this.data.lesstime)
  },

  appointment_handler:function(){
    this.setData({ showAppointment:false});
  },
  select_wallet:function(e){
    var index = e.currentTarget.dataset.id;
    var val = e.currentTarget.dataset.data;
    if(index == 4){
      this.setData({
        is_show:true
      })
    }else{
      this.setData({
        is_show:false
      })
    }
    this.setData({ wallet_id: e.currentTarget.dataset.id, dsprice: val});
    var less_price = this.data.less_price;
    var dsprice = this.data.dsprice;
    var total = parseFloat(less_price) + parseFloat(dsprice);
    this.setData({
      total_price: total ? total : 0
    })
     
    // 
  },
  //自定义金额
  zdyPrice:function(e){
    var price = e.detail.value;
    this.setData({
      dsprice:price
    })
    var less_price = this.data.less_price;
    var dsprice = this.data.dsprice;
    var total = parseFloat(less_price) + parseFloat(dsprice);
    this.setData({
      total_price: total ? total:0
    })
  },
 

  less_address:function(e){
    var less_address = e.detail.value;
    this.setData({
      less_address: less_address
    })
  },
  initData: function (tid) {
    var para = {
      tearch_id:tid,
    };
    var that = this;
    handler.requestData("appointment", para, function (res) {
      if (res.success == true) {
        that.setData({
          spinShow: false,
          tearcherData: res.data,
          less_price: res.data.less_price,
          total_price: parseFloat(res.data.less_price) + parseFloat(that.data.dsprice)
        })
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
      
    });
  },

  // pay-button 支付按钮
  pay_button:function(){
    var userType = wx.getStorageSync('user_info').user_type;
    if (userType == 1){//导师
      $Message({
        content: '导师不能预约导师',
        type: 'error'
      });
      return;
    }
    var submitJson ={
      user_name: this.data.name,
      user_id :wx.getStorageSync('user_info').id,
      phone: this.data.phone,
      tearch_id:this.data.tearch_id,
      less_time: this.data.lesstime,
      less_address: this.data.less_address,
      less_price:this.data.less_price, //课时费
      exceptional_price: this.data.dsprice, //打赏金额
      openid:wx.getStorageSync('user_info').openid
    };
    var that = this;
    handler.requestData("userAppointment", submitJson, function (res) {
      
      if (res.success == true) {
        $Message({
          content: res.message,
          type: 'success'
        });
        that.setData({
          payPara:res.data
        })
        that.handleOpen5()
      } else {
        $Message({
          content: res.message,
          type: 'error'
        });
      }
    });
  },
  handleOpen5() {
    this.setData({
      visible5: true
    });
  },

  handleClick5({ detail }) {
    if (detail.index === 0) {
      this.setData({
        visible5: false
      });
      handler.intoPageHandler("../user-appointment-record/index");
    } else {
      const action = [...this.data.actions5];
      // action[1].loading = true;

      this.setData({
        actions5: action
      });
      wx.requestPayment({
        'timeStamp': '' + this.data.payPara.timeStamp + '',
        'nonceStr': '' + this.data.payPara.nonceStr + '',
        'package': '' + this.data.payPara.package + '',
        'signType': 'MD5',
        'paySign': '' + this.data.payPara.paySign + '',
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '支付成功',
            icon: 'loading'
          })
          handler.intoPageHandler("../user-appointment-record/index");
        },
        fail: function (res) {
          wx.showToast({
            title: '支付失败',
            icon: 'loading'
          })

        }
      })

      // handler.newPayHandler(this.data.payPara);
      // setTimeout(() => {
      //   action[1].loading = false;
      //   this.setData({
      //     visible5: false,
      //     actions5: action
      //   });
      //   $Message({
      //     content: '支付成功！',
      //     type: 'success'
      //   });
      //   handler.intoPageHandler("../user-appointment-record/index"); 
      // }, 2000);
    }
  },
  cancleAppointment:function(e){
    this.setData({ showAppointment:true})
  },
  // 获取用户名
  get_name:function(e){
    // var test_name=handler.regTest(e.detail.value,"请输入用户名");
    // test_name && this.setData({ name: e.detail.value});
    this.setData({ name: e.detail.value });
  },
  // 获取电话号码
  get_phone:function(e){
    this.setData({ phone: e.detail.value })
    // var test_phone = handler.regTest(e.detail.value, "请输入正确的电话号码", /^1\d{10}$/);
    // test_phone && this.setData({ phone: e.detail.value });
  },
  //时间样式开始
  selectTimeShowTimeHandler: function () {
    this.setData({ showTimeContainer: true, homeHandlerShow: false });
    this.selectTimeHandler(0, 0, 0);
  },
  // getTimeHandlerCancle 预约时间取消样式
  getTimeHandlerCancle: function () {
    this.setData({ showTimeContainer: false, homeHandlerShow: true });
  },
  // getTimeHandlerOk 预约时间确定样式
  getTimeHandlerOk: function () {
    wx.getS
    this.setData({ showTimeContainer: false, homeHandlerShow: true });
  },
  // 绑定时间定时器
  bindTimeChange: function (e) {
    console.log(e.detail.value);
    // 今天样式
    if (e.detail.value[0] === 0 && e.detail.value[1] === 0) {
      this.selectTimeHandler(0, 0);
    } else if (e.detail.value[0] === 0 && e.detail.value[1] !== 0) {
      this.selectTimeHandler(0, e.detail.value[1]);
    }
    // 明天和后台样式
    else if (e.detail.value[0] !== 0) {
      this.selectTimeHandler(e.detail.value[0], e.detail.value[1], 0);
    }
  },
  // 选择时间函数
  selectTimeHandler: function (dateParams, hoursParams, minutesParams) {
    const { hours, minute } = this.data;
    // console.log(dateParams);
    const data = new Date();
    const hour = data.getHours();
    // 今天样式
    hours.length = 0;
    minute.length = 0;
    // console.log(dateParams, hoursParams, minutesParams);
    if (dateParams === 0 && hoursParams === 0) {

      hours.push("现在");
      for (var i = hour + 1; i < 24; i++) {
        hours.push(i + "点");
      }
      this.setData({ hours, minute });
    } else if (dateParams === 0 && hoursParams !== 0) {
      hours.push("现在");
      for (var i = hour + 1; i < 24; i++) {
        hours.push(i + "点");
      }
      for (var i = 0; i < 60; i += 10) {
        minute.push(i);
      }
      this.setData({ hours, minute });
    } else if (dateParams !== 0) {
      for (var i = 0; i < 24; i++) {
        hours.push(i + "点");
      }
      for (var i = 0; i < 60; i += 10) {
        minute.push(i);
      }
      this.setData({ hours, minute });
    }
  },
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
   
    var time = this.data.dateTimeArray1[0][this.data.dateTime1[0]] + "-" + this.data.dateTimeArray1[1][this.data.dateTime1[1]] + "-" + this.data.dateTimeArray1[2][this.data.dateTime1[2]] + " " + this.data.dateTimeArray1[3][this.data.dateTime1[3]] + ":" + this.data.dateTimeArray1[4][this.data.dateTime1[4]];

    var timestamp2 = Date.parse(new Date(time));
    timestamp2 = timestamp2 / 1000;

    this.setData({ lesstime: timestamp2})
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
    arr[e.detail.column] = e.detail.value;

    dateArr[2] = handler.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });

    var time = this.data.dateTimeArray1[0][this.data.dateTime1[0]] + "-" + this.data.dateTimeArray1[1][this.data.dateTime1[1]] + "-" + this.data.dateTimeArray1[2][this.data.dateTime1[2]] + " " + this.data.dateTimeArray1[3][this.data.dateTime1[3]] + ":" + this.data.dateTimeArray1[4][this.data.dateTime1[4]];

    var timestamp2 = Date.parse(new Date(time));
    timestamp2 = timestamp2 / 1000;
   

    this.setData({ lesstime: timestamp2 })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {
  //   console.log('1110')
  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})